package com.mo.ld.activity.animation

import android.animation.Animator
import android.animation.AnimatorSet
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import com.mo.ld.bean.ActionBean
import com.mo.ld.databinding.ActObjectAnimationBinding
import com.mo.ld.modle.getMainAdapter2
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.anim.*
import com.mo.lib.utils.setClick
import com.mo.lib.view.recyclerview.layoutG

/**
 * @ author：mo
 * @ data：2022/3/28:10:01
 * @ 功能：
 */
class ObjectAnimatorActivity : BaseVbActivity<ActObjectAnimationBinding>() {

    override fun initData() {
        viewBinding.krvObject.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("平移(左至右)") {
                viewBinding.tvObject.objectAnimatorTranslationX(0f, -viewBinding.tvObject.left.toFloat(), viewBinding.tvObject.width.toFloat(), 0f).setDuration(2000).start()
            },
            ActionBean("平移(右至左)") {
                viewBinding.tvObject.objectAnimatorTranslationX(0f, viewBinding.tvObject.right.toFloat(), -viewBinding.tvObject.width.toFloat(), 0f).setDuration(2000).start()
            },
            ActionBean("平移(下至上)") {
                viewBinding.tvObject.objectAnimatorTranslationY(0f, viewBinding.tvObject.bottom.toFloat(), -viewBinding.tvObject.height.toFloat(), 0f).setDuration(2000).start()
            },
            ActionBean("平移(上至下)") {
                viewBinding.tvObject.objectAnimatorTranslationY(0f, -viewBinding.tvObject.bottom.toFloat(), viewBinding.tvObject.height.toFloat(), 0f).setDuration(2000).start()
            },
            ActionBean("旋转(Z轴)") {
                viewBinding.tvObject.objectAnimatorRotationZ(0f, -180f, 360f).setDuration(2000).start()
            },
            ActionBean("旋转(X轴)") {
                viewBinding.tvObject.objectAnimatorRotationX(0f, -180f, 360f).setDuration(2000).start()
            },
            ActionBean("旋转(Y轴)") {
                viewBinding.tvObject.objectAnimatorRotationY(0f, -180f, 360f).setDuration(2000).start()
            },
            ActionBean("缩放(X轴)") {
                viewBinding.tvObject.objectAnimatorScaleX(1f, 3f, 0.5f, 1f).setDuration(2000).start()
            },
            ActionBean("缩放(Y轴)") {
                viewBinding.tvObject.objectAnimatorScaleY(1f, 3f, 0.5f, 1f).setDuration(2000).start()
            },
            ActionBean("透明度") {
                viewBinding.tvObject.objectAnimatorAlpha(1f, 0f, 1f).setDuration(2000).start()
            },
            ActionBean("心跳") {
                val animatorSet = AnimatorSet()
                val objectAnimatorScaleX = viewBinding.tvObject.objectAnimatorScaleX(1f, 1.5f, 0.7f, 1f)
                objectAnimatorScaleX.repeatCount = 5
                val objectAnimatorScaleY = viewBinding.tvObject.objectAnimatorScaleY(1f, 1.5f, 0.7f, 1f)
                objectAnimatorScaleY.repeatCount = 5
                animatorSet.play(objectAnimatorScaleX).with(objectAnimatorScaleY)
                animatorSet.duration = 2000
                animatorSet.interpolator = LinearInterpolator()
                animatorSet.start()
            },
        ))
        viewBinding.krvView.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("平移(左)") {
                viewBinding.tvObject.animate().translationXBy(-40f).duration = 1000
            },
            ActionBean("平移(右)") {
                viewBinding.tvObject.animate().translationXBy(40f).duration = 1000
            },
            ActionBean("平移(上)") {
                viewBinding.tvObject.animate().translationYBy(-40f).duration = 1000
            },
            ActionBean("平移(下)") {
                viewBinding.tvObject.animate().translationYBy(40f).duration = 1000
            },
            ActionBean("旋转(Z轴)") {
                viewBinding.tvObject.animate().rotationBy(45f).duration = 1000
            },
            ActionBean("旋转(X轴)") {
                viewBinding.tvObject.animate().rotationXBy(45f).duration = 1000
            },
            ActionBean("旋转(Y轴)") {
                viewBinding.tvObject.animate().rotationYBy(45f).duration = 1000
            },
            ActionBean("缩放(大/X轴)") {
                viewBinding.tvObject.animate().scaleXBy(0.5f).duration = 1000
            },
            ActionBean("缩放(大/Y轴)") {
                viewBinding.tvObject.animate().scaleYBy(0.5f).duration = 1000
            },
            ActionBean("缩放(小/Y轴)") {
                viewBinding.tvObject.animate().scaleYBy(-0.5f).duration = 1000
            },

            ActionBean("缩放(小/Y轴)") {
                viewBinding.tvObject.animate().scaleYBy(-0.5f).duration = 1000
            },
            ActionBean("透明度") {
                viewBinding.tvObject.animate().alphaBy(-0.5f).duration = 1000
            },
            ActionBean("心跳") {
                val anim = viewBinding.tvObject.animate()
                anim.scaleX(2.0f).scaleY(2.0f).setInterpolator(AccelerateInterpolator()).setDuration(200).setListener(object : Animator.AnimatorListener {
                    var tag = true
                    override fun onAnimationRepeat(animation: Animator) {}

                    override fun onAnimationEnd(animation: Animator) {
                        if (tag) {
                            anim.scaleX(1.0f).scaleY(1.0f).setInterpolator(DecelerateInterpolator()).setDuration(400).startDelay = 0
                        } else {
                            anim.scaleX(2.0f).scaleY(2.0f).setInterpolator(AccelerateInterpolator()).setDuration(400).startDelay = 2000
                        }
                        tag = !tag
                    }

                    override fun onAnimationCancel(animation: Animator) {}

                    override fun onAnimationStart(animation: Animator) {}
                }).startDelay = 500
            },
        ))
        viewBinding.tvTranslate.setClick {
            viewBinding.tvObject.visibility = View.INVISIBLE
        }
        viewBinding.krvTranslate.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("底部进入") {
                viewBinding.tvObject.startAnimation(getTranslateAnimationBottom2Self().setDurations(1000))
            },
            ActionBean("顶部进入") {
                viewBinding.tvObject.startAnimation(getTranslateAnimationTop2Self().setDurations(1000))
            },
            ActionBean("左侧进入") {
                viewBinding.tvObject.startAnimation(getTranslateAnimationSelf2Left().setDurations(1000))
            },
            ActionBean("右侧进入") {
                viewBinding.tvObject.startAnimation(getTranslateAnimationSelf2Right().setDurations(1000))
            },
        ))
        viewBinding.krvScale.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("放大") {
                viewBinding.tvObject.startAnimation(getScaleAnimationAnimationBig(1f).setDurations(1000))
            },
            ActionBean("缩小") {
                viewBinding.tvObject.startAnimation(getScaleAnimationAnimationSmall(1f).setDurations(1000))
                viewBinding.tvObject.visibility = View.INVISIBLE
            },
        ))
        viewBinding.krvRotate.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("转一圈") {
                viewBinding.tvObject.startAnimation(getRotateAnimationAnimationAnimationSelf().setDurations(1000))
            },
        ))
        viewBinding.krvAlpha.layoutG(4).adapter = getMainAdapter2(mutableListOf(
            ActionBean("可见变不可见") {
                viewBinding.tvObject.startAnimation(getAlphaAnimationAnimationAnimationAnimationSelf(1f, 0f).setDurations(1000))
            },
            ActionBean("不可见变可见") {
                viewBinding.tvObject.startAnimation(getAlphaAnimationAnimationAnimationAnimationSelf(0f, 1f).setDurations(1000))
            },
        ))
    }

}